package com.lovo.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.bean.Student;
import com.lovo.bean.vo.ResultVO;
import com.lovo.config.ApplicationContextFactory;
import com.lovo.service.StudentService;
import com.lovo.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addStudent")
public class AddStudentServlet extends HttpServlet {
    private StudentService studentService = (StudentService) ApplicationContextFactory.getBean("studentService");
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Student student = new Student();

        student.setName(req.getParameter("name"));
        student.setGender(req.getParameter("gender"));
        student.setAge(Integer.parseInt(req.getParameter("age")));
        studentService.insert(student);
        ResultVO resultVO = new ResultVO();
        ObjectMapper objectMapper = new ObjectMapper();
        resp.getWriter().println(objectMapper.writeValueAsString(resultVO));
    }
}
